import { Component } from '@angular/core';
import { Platform, NavController, AlertController, App } from 'ionic-angular';

import { Fun } from '../../providers/fun';
import { TabsPage } from '../tabs/tabs';
import { WeChatApi } from '../../providers/wechatapi';
import { PubApi } from '../../providers/pubapi';





declare var echarts;
declare var $;

@Component({
    selector: 'page-login',
    templateUrl: 'login.html'
})

export class LoginPage extends Fun {
    Phone: any;
    PassWord: any
    constructor(public platform: Platform, public alertCtrl: AlertController, public appCtrl: App, public navCtrl: NavController, public wechatapi: WeChatApi, public pubapi: PubApi) {
        super(alertCtrl);
        platform.ready().then(() => {

            //    var that = this;
          

        });


    }

    ionViewDidEnter() {


    }

    Login() {
        if ((<any>window).tag == 1)
        { }
        else {

            if (Fun.isNullOrEmpty(this.Phone)) {
                this.showAlert("手机号码不能为空！");
            }
            else if (!Fun.checkPhone(this.Phone)) {
                this.showAlert("手机号码格式不正确！");
            }
            else if (Fun.isNullOrEmpty(this.PassWord)) {
                this.showAlert("密码不能为空！");
            }
            else if (this.PassWord == "1") {

                Fun.setUser("testUserKey");
               
            }
            else {
                var that = this;
               // (<any>window).openid = "12";//test
                this.pubapi.get("Users/MobileLogin?openId=" + (<any>window).openid + "&phone=" + this.Phone + "&password=" + this.PassWord + "", function (res) {
                    let r = res.data;
                    if (r) {
                        LoginPage.setUser("1");
                        LoginPage.setCookie("phone", that.Phone);
                        LoginPage.setCookie("name", r.Name);
                        LoginPage.setCookie("Position", r.Position);
                        LoginPage.setCookie("key", r.Key);
                        
                         that.navCtrl.push(TabsPage, {});
                    }
                    else
                    {
                          that.showAlert("账号或密码错误！");

                    }
                })
              
            }
            //(<any>window).openid
            // var result = {
            //     openid: "123",
            //     company: this.Phone
            // }

            // this.wechatapi.postData(result).subscribe((res) => {
            //     console.log(res);
            // })
        }



    }

   
}

